﻿// CModifyDlg.cpp: 实现文件
//

#include "pch.h"
#include "CClient.h"
#include "CModifyDlg.h"
#include "afxdialogex.h"


// CModifyDlg 对话框

IMPLEMENT_DYNAMIC(CModifyDlg, CDialogEx)

CModifyDlg::CModifyDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_INPUT, pParent)
{

}

CModifyDlg::~CModifyDlg()
{
}

void CModifyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CModifyDlg, CDialogEx)
	ON_BN_CLICKED(IDC_INPUT_ADD, &CModifyDlg::OnClickedModify)
	ON_BN_CLICKED(IDC_INPUT_CANCEL, &CModifyDlg::OnClickedCancel)
END_MESSAGE_MAP()


// CModifyDlg 消息处理程序


BOOL CModifyDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	SetDlgItemText(IDC_INPUT_NUM, num);
	SetDlgItemText(IDC_INPUT_NAME, name);
	SetDlgItemText(IDC_INPUT_SALARY, salary);

	CDateTimeCtrl* cDate = (CDateTimeCtrl*)GetDlgItem(IDC_INPUT_DATE);
	COleDateTime time;
	time.ParseDateTime(date);// 支持一些常用的日期转换
	cDate->SetTime(time);

	GetDlgItem(IDC_INPUT_NUM)->EnableWindow(FALSE);
	SetDlgItemText(IDC_INPUT_ADD, _T("确定"));

	return TRUE;
}


void CModifyDlg::OnClickedModify()
{
	// 走回车键相同的逻辑
	OnOK();
}


void CModifyDlg::OnClickedCancel()
{
	EndDialog(IDCANCEL);
}


void CModifyDlg::OnOK()
{
	auto pWnd = GetFocus();
	int id = pWnd->GetDlgCtrlID();
	if (id == IDC_INPUT_DATE || id == IDC_INPUT_ADD) {
		ModifyData();
	}
	else {
		NextDlgCtrl();
	}
}

void CModifyDlg::ModifyData()
{
	int cmd = WK_MODIFY;
	SInfo d;
	CString str;

	GetDlgItemText(IDC_INPUT_NUM, str);
	d.nNumb = _tstoi(str);

	GetDlgItemText(IDC_INPUT_NAME, str);
	std::string s = theApp.UnicodeToUTF8(str);
	strcpy_s(d.sName, _countof(d.sName), s.c_str());// 安全版strcpy

	GetDlgItemText(IDC_INPUT_SALARY, str);
	d.fSala = _tstof(str);

	GetDlgItemText(IDC_INPUT_DATE, str);
	s = theApp.UnicodeToUTF8(str);
	strcpy_s(d.sDate, _countof(d.sDate), s.c_str());

	theApp.m_sock.Send(&cmd, sizeof(cmd));
	theApp.m_sock.Send(&d, sizeof(d));

	AfxMessageBox(_T("修改成功"));
	EndDialog(IDOK);
}
